package com.company;

import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Fun_sel extends JPanel {
    private JXDatePicker jdp1, jdp2;
    private JComboBox<String> box, statusBox;
    private JTextField t1, t2, t3, t4, t5;
    String[] strings = new String[]{"单号:", "班次:", "班组:", "操作员:", "操作员编号:"};
    String type, s1, s2, s3, s4, s5, s6, time1, time2;
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
    List<Rt> data = new ArrayList<>();
    RtModel rtModel;
    JTable jTable;

    public Fun_sel() {
        super(new BorderLayout());
        JLabel filler = new JLabel("查询及导出");
        filler.setFont(new Font("楷体", Font.TYPE1_FONT, 20));
        filler.setForeground(Color.RED);
        add(getTopPanel(), BorderLayout.NORTH);
        add(getCenterPanel(), BorderLayout.CENTER);
    }

    public JPanel getCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        rtModel = new RtModel(data);
        jTable = new JTable(rtModel);
        JScrollPane js = new JScrollPane(jTable);
        panel.add(js);
        return panel;
    }

    public JPanel getTopPanel() {
        JPanel panel = new JPanel();
        TitledBorder titledBorder = BorderFactory.createTitledBorder("查询条件");
        titledBorder.setTitleJustification(TitledBorder.TOP);
        panel.setBorder(titledBorder);
        panel.setLayout(new BorderLayout());
        panel.add(getBox(), BorderLayout.NORTH);
        panel.add(getCenter());
        return panel;
    }

    public JPanel getBox() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 6, 5, 50));
        box = new JComboBox<>();
        box.addItem("勾核");
        box.addItem("配发");
        panel.add(box);
        panel.add(getStartTime());
        panel.add(getEndTime());
        panel.add(new JPanel());
        JButton button = new JButton("查询");
        panel.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                s1 = t1.getText();
                s2 = t2.getText();
                s3 = t3.getText();
                s4 = t4.getText();
                s5 = t5.getText();
                s6 = statusBox.getSelectedItem().toString();
                type = box.getSelectedItem().toString();
                time1 = sf.format(jdp1.getDate());
                time2 = sf.format(jdp2.getDate());
                Thread t = new Thread() {
                    @Override
                    public void run() {
                        List<Rt> rts = SqliteTools.queryData(type, s1, s6, s2, s3, s4, s5, time1, time2);
                        if (rts != null) {
                            data.clear();
                            data.addAll(rts);
                            SwingUtilities.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    rtModel.fireTableDataChanged();
                                }
                            });
                        }
                    }
                };
                t.setPriority(Thread.MAX_PRIORITY);
                t.start();
            }
        });
        JButton button1 = new JButton("导出");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (data.size() == 0) {
                    JOptionPane.showMessageDialog(null, "暂无导出数据，清先查询相关数据");
                    return;
                }
                export();
            }
        });
        panel.add(button1);
        return panel;
    }

    private JPanel getStartTime() {
        JPanel jPanel = new JPanel();
        JLabel label = new JLabel("从");
        jPanel.add(label);
        jdp1 = new JXDatePicker();
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -30);
        jdp1.setDate(now.getTime());
        jPanel.add(jdp1);
        return jPanel;
    }

    private JPanel getEndTime() {
        JPanel jPanel = new JPanel();
        JLabel label = new JLabel("到");
        jPanel.add(label);
        jdp2 = new JXDatePicker();
        jdp2.setDate(new Date());
        jPanel.add(jdp2);
        return jPanel;
    }

    private JPanel getCenter() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 1, 1, 1));
        panel.add(getInput1());
        panel.add(getInput2());
        panel.add(getInput3());
        return panel;
    }

    private JPanel getInput1() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 3, 1, 1));
        JPanel j1 = new JPanel();
        j1.setLayout(new BorderLayout());
        j1.add(getLablePanel(strings[0]), BorderLayout.WEST);
        panel.add(j1);
        JPanel j2 = new JPanel();
        j2.setLayout(new BorderLayout());
        j2.add(getLablePanel(strings[1]), BorderLayout.WEST);
        panel.add(j2);
//        panel.add(new JPanel());
        return panel;
    }

    private JPanel getInput2() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 3, 1, 1));
        JPanel j1 = new JPanel();
        j1.setLayout(new BorderLayout());
        j1.add(getLablePanel(strings[2]), BorderLayout.WEST);
        panel.add(j1);
        JPanel j2 = new JPanel();
        j2.setLayout(new BorderLayout());
        j2.add(getLablePanel(strings[3]), BorderLayout.WEST);
        panel.add(j2);
//        panel.add(new JPanel());
        return panel;
    }

    private JPanel getInput3() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 3, 1, 1));
        JPanel j1 = new JPanel();
        j1.setLayout(new BorderLayout());
        j1.add(getLablePanel(strings[4]), BorderLayout.WEST);
        panel.add(j1);
        JPanel j2 = new JPanel();
        j2.setLayout(new BorderLayout());
        JPanel j = new JPanel();
        JLabel jLabel = new JLabel("状态:");
        JPanel jj = new JPanel();
        jj.setPreferredSize(new Dimension(70, 30));
        jj.add(jLabel);
        j.add(jj);
        statusBox = new JComboBox<>();
        statusBox.addItem("全部");
        statusBox.addItem("正常");
        statusBox.addItem("异常");
        j.add(statusBox);
        j2.add(j, BorderLayout.WEST);
        panel.add(j2);
//        panel.add(new JPanel());
        return panel;
    }

    private JPanel getLablePanel(String text) {
        JPanel panel = new JPanel();
        JLabel label = new JLabel(text);
//        label.setForeground(Color.RED);
        JPanel j = new JPanel();
        j.setPreferredSize(new Dimension(70, 30));
        j.add(label);
        panel.add(j);

        panel.add(getTextFieldPanel(text));
        return panel;
    }

    private JPanel getTextFieldPanel(String text) {
        JPanel panel = new JPanel();
        JTextField label = new JTextField();
        label.setColumns(20);
        panel.add(label);
        if (text.equals(strings[0])) {
            t1 = label;
        } else if (text.equals(strings[1])) {
            t2 = label;
        } else if (text.equals(strings[2])) {
            t3 = label;
        } else if (text.equals(strings[3])) {
            t4 = label;
        } else if (text.equals(strings[4])) {
            t5 = label;
        }
        return panel;
    }

    private void export() {
        FileDialog fd = new FileDialog(MainContent.getjFrame(), "保存单号信息", FileDialog.SAVE);
        fd.setLocation(500, 350);
        fd.setVisible(true);
        String stringfile = fd.getDirectory() + fd.getFile() + ".xls";
        try {
            ExcelExporter export = new ExcelExporter();
            export.exportTable(jTable, new File(stringfile));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
